﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Script.Serialization;
using WebBHXH.Entities;
using System.IO;

namespace WebBHXH
{
    public partial class AddDotBH : System.Web.UI.Page
    {
        List<NguoiSudungModel> listNguoiSudungModel;
        List<MucBHModel> listMucBHModel;
        List<LoaiBHModel> listLoaiBHModel;
        List<HuyenModel> listHuyenModel;
        List<TinhModel> listTinhModel;
        List<DonViBHModel> listDonViBHModel;
        string id="0";
        protected void Page_Load(object sender, EventArgs e)
        {
            GetDonViBHModel();
            GetTinhModel();
            GetHuyenModel();
            GetLoaiBHModel();
            GetMucBHModel();
            GetNguoiSudungModel();
            if (!IsPostBack)
            {
                fillDropdown();
                if (Request.QueryString["Id"] != null)
                {
                    id = Request["id"];
                    MaDotBHModel maDotBHModel = new MaDotBHModel().GetDotBHById(id);
                    
                    DropDownList1.SelectedIndex = listDonViBHModel.FindIndex(delegate(DonViBHModel donViBHModel)
                    {
                        return donViBHModel.MaDonViBH.Equals(maDotBHModel.DonViBH, StringComparison.Ordinal);
                    }) +1 ;

                    DropDownList2.SelectedIndex = listTinhModel.FindIndex(delegate(TinhModel tinhModel)
                    {
                        return tinhModel.MaTinh.Equals(maDotBHModel.MaTinh);
                    }) + 1;

                    DropDownList3.SelectedIndex = listHuyenModel.FindIndex(delegate(HuyenModel huyenModel)
                    {
                        return huyenModel.MaHuyen.Equals(maDotBHModel.Huyen);
                    }) + 1;

                    DropDownList5.SelectedIndex = listLoaiBHModel.FindIndex(delegate(LoaiBHModel loaiBHModel)
                    {
                        return loaiBHModel.MaLoaiBH.Equals(maDotBHModel.LoaiBH);
                    }) + 1;

                    DropDownList6.SelectedIndex = listMucBHModel.FindIndex(delegate(MucBHModel mucBHModel)
                    {
                        return mucBHModel.MaMucBH.Equals(maDotBHModel.MucBH);
                    }) + 1;

                    txtObject.Text = maDotBHModel.DoiTuong;
                    dpkDate.SelectedDate = maDotBHModel.NgayLap;
                    txtTien1Nam.Text = maDotBHModel.SoTien1Nam.ToString();

                    btnDangKy.Text = "Cập Nhật";
                }
                else
                {
                    txtId.Visible = false;
                    Label1.Visible = false;
                    btnDangKy.Text = "Đăng Ký";
                }
                txtId.Text = id;
            }
        }

        public void fillDropdown()
        {
            DropDownList6.Items.Add("-- Chọn Mục BH --");
            foreach (MucBHModel mucBHModel in listMucBHModel)
            {
                DropDownList6.Items.Add(mucBHModel.TenMucBH);
            }
            DropDownList5.Items.Add("-- Chọn Loại BH --");
            foreach (var loaiBHModel in listLoaiBHModel)
            {
                DropDownList5.Items.Add(loaiBHModel.TenLoaiBH);
            }
            DropDownList3.Items.Add("-- Chọn Tên Huyện --");
            //foreach (HuyenModel huyenModel in listHuyenModel)
            //{
            //    DropDownList3.Items.Add(huyenModel.TenHuyen);
            //}
            DropDownList2.Items.Add("-- Chọn Tên Tỉnh --");
            foreach (TinhModel tinhModel in listTinhModel)
            {
                DropDownList2.Items.Add(tinhModel.TenTinh);
            }
            DropDownList1.Items.Add("-- Chọn Tên Đơn Vị --");
            foreach (DonViBHModel donViBHModel in listDonViBHModel)
            {
                DropDownList1.Items.Add(donViBHModel.TenDonViBH);
            }

        }

        private void GetNguoiSudungModel()
        {
            listNguoiSudungModel = new NguoiSudungModel().GetNguoiSudungModel();
        }

        private void GetMucBHModel()
        {
            listMucBHModel = new MucBHModel().GetMucBHModel();
        }

        private void GetLoaiBHModel()
        {
            listLoaiBHModel = new LoaiBHModel().GetLoaiBHModel();
        }

        private void GetHuyenModel()
        {
            listHuyenModel = new HuyenModel().GetHuyenModel();
        }

        private void GetTinhModel()
        {
            listTinhModel = new TinhModel().GetTinhModel();
        }

        private void GetDonViBHModel()
        {
            listDonViBHModel = new DonViBHModel().GetDonViBHModel();
        }

        protected void btnDangKy_Click(object sender, EventArgs e)
        {
            string maDonviBH = listDonViBHModel[DropDownList1.SelectedIndex-1].MaDonViBH;
            string maTinh = listTinhModel[DropDownList2.SelectedIndex-1].MaTinh;
            string huyen = listHuyenModel[DropDownList3.SelectedIndex-1].MaHuyen;
            string doiTuong = txtObject.Text;
            string ngayLap = dpkDate.SelectedDate.ToShortDateString();
            string loaiBaoHiem = listLoaiBHModel[DropDownList5.SelectedIndex-1].MaLoaiBH;
            string mucBaohiem = listMucBHModel[DropDownList6.SelectedIndex-1].MaMucBH;
            string soTien1Nam = txtTien1Nam.Text;
            string nguoiSudung = Session["id"].ToString();
            System.Net.WebRequest request = null;
            if (txtId.Text == "0")
            {
                request = System.Net.WebRequest.Create(""+Util.getUrl() + "Home/insertDotBaoHiem?DonViBH=" + maDonviBH + "&MaTinh=" + maTinh + "&Huyen=" + huyen + "&DoiTuong=" + doiTuong + "&NgayLap=" + ngayLap + "&LoaiBH=" + loaiBaoHiem + "&MucBH=" + mucBaohiem + "&SoTien1Nam=" + soTien1Nam + "&NguoiSudung=" + nguoiSudung);
            }
            else {
                request = System.Net.WebRequest.Create(""+Util.getUrl() + "Home/updateDotBaoHiem?MaDotBH=" + txtId.Text + "&DonViBH=" + maDonviBH + "&MaTinh=" + maTinh + "&Huyen=" + huyen + "&DoiTuong=" + doiTuong + "&NgayLap=" + ngayLap + "&LoaiBH=" + loaiBaoHiem + "&MucBH=" + mucBaohiem + "&SoTien1Nam=" + soTien1Nam + "&NguoiSudung=" + nguoiSudung);

            }
            request.Method = "POST";
            request.ContentLength = 0;
            request.ContentType = "application/json; charset=utf-8";
            Stream s = request.GetResponse().GetResponseStream();
            StreamReader sr = new StreamReader(s);
            string result = sr.ReadToEnd();
            if (result == "200")
            {
                
            }
            else
            {
               
            }

            Response.Redirect("DotBH.aspx");
        }

        protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
        {
            string maTinh = "0";
            listHuyenModel = new List<HuyenModel>();
            if (DropDownList2.SelectedIndex > 0)
            {
                maTinh = listTinhModel[DropDownList2.SelectedIndex - 1].MaTinh;
                listHuyenModel = new HuyenModel().GetHuyenByTinh(maTinh);
            }
            DropDownList3.Items.Clear();
            DropDownList3.Items.Add("-- Chọn Tên Huyện --");
            foreach (HuyenModel huyenModel in listHuyenModel)
            {
                DropDownList3.Items.Add(huyenModel.TenHuyen);
            }
        }
    }
}